package com.active.interceptor;

import com.base.dto.Page;
import com.base.dto.PageContent;
import com.base.util.mybatis.PageHelper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class PageInterceptor {

    private static final Logger logger = LoggerFactory.getLogger(PageInterceptor.class);

    @Around("execution (* com.*.service.*.*Page(..))")
    public Object createPage(ProceedingJoinPoint joinPoint) throws Exception{
        logger.info("【执行分页查询】");
        Object []args = joinPoint.getArgs();
        Page page=null;
        if(args!=null && args.length>0){
            for(int i=0;i<args.length;i++){
                if(args[i] instanceof Page){
                    page = (Page) args[i];
                }
            }
        }
        if(page!=null){
            PageHelper.startPage((PageContent) page);
        }
        try {
            return joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            throw new Exception(throwable);
        }finally {
            PageHelper.endPage();
        }

    }

}
